package com.argrace.smart.base;

import android.app.Activity;
import android.content.Context;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by argrace_dev_1 on 2018/1/30.
 */

public class ActivityManager {
    /**
     * 存放Activity的map
     */
    private static Map<String, Activity> activitys = new HashMap<String, Activity>();

    private static Map<String, Integer> activityStatus = new HashMap<String, Integer>();


    /**
     * 获取管理类中注册的所有Activity的map
     *
     * @return
     */
    public static Map<String, Activity> getActivitys() {
        return activitys;
    }


    /**
     * 根据键值取对应的Activity
     *
     * @param key 键值
     * @return 键值对应的Activity
     */
    public static Activity getActivity(String key) {
        return activitys.get(key);
    }


    /**
     * 注册Activity
     *
     * @param value
     * @param key
     */
    public static void addActivity(Activity value, String key) {
        activitys.put(key, value);
    }


    public static void updateActivity(String key, Integer status) {
        activityStatus.put(key, status);
    }


    /**
     * 将key对应的Activity移除掉
     *
     * @param key
     */
    public static void removeActivity(String key) {
        activitys.remove(key);
    }


    /**
     * finish掉所有的Activity移除所有的Activity
     */
    public static void removeAllActivity() {
        Iterator<Activity> iterActivity = activitys.values().iterator();
        while (iterActivity.hasNext()) {
            iterActivity.next().finish();
        }
        activitys.clear();
    }

    public static boolean AllActivityOnPauseStatus() {
        boolean isActivityStatus = false;
        boolean isFragmentActivityStatus = false;
        boolean isStatus = false;
        Iterator<Integer> iterActivity = activityStatus.values().iterator();
        while (iterActivity.hasNext()) {

            if (iterActivity.next() != 1) {
                isActivityStatus = true;
            } else {
                isActivityStatus = false;
            }
        }

        if (isActivityStatus) {
            isStatus = true;
        } else {
            isStatus = false;
        }

        return isStatus;
    }

    public static String getTopActivity(Context context)

    {

        android.app.ActivityManager manager = (android.app.ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        List<android.app.ActivityManager.RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
        android.app.ActivityManager.RunningTaskInfo info = manager.getRunningTasks(1).get(0);
        if (runningTaskInfos != null)

            // return (runningTaskInfos.get(0).topActivity).toString();
            return info.topActivity.getClassName();

        else

            return null;

    }

    public static int getActivityStatus(String ClassName) {
        return activityStatus.get(ClassName);
    }


}
